﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ElectronicJournal.Account.AuthorAccount;
using System.Net.Mail;
using System.Net;
using System.Web.Security;

namespace ElectronicJournal
{
    public class SpamLord
    {
        public SpamLord()
        {
            NewPublication.SendingDelegateEvent += new SendingDelegate(NewPublication_SendingDelegateEvent);
            MyAccount.ChangingStateDelegateEvent +=new ChangingStateDelegate(MyAccount_ChangingStateDelegateEvent);
        }

        private void MyAccount_ChangingStateDelegateEvent(Guid authorguid, string statepub, Guid pubid)
        {
            Author currentAuthor = EJournalRepository.Instance.GetAuthorByUserId(authorguid);
            var currentPub = EJournalRepository.Instance.GetPublicationById(pubid);
            SendToMail(currentAuthor.AuthorEmail, "Текущее состояние публикации " + currentPub.TitleRussian + " изменено на " + statepub);
        }

        protected void NewPublication_SendingDelegateEvent(Guid userGuid, string target)
        {
            Author currentAuthor = EJournalRepository.Instance.GetAuthorByUserId(userGuid);
            SendToMail(currentAuthor.AuthorEmail, "Вы только что подали статью " + target + " в электронный журнал \"Открытое образование\"");
        }


        protected void SendToMail(string email, string content)
        {
            string From = "ElectronicJournalSite@gmail.com";
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential("ElectronicJournalSite@gmail.com", ";ehyfk*()");

            string to = email;
            MailMessage message = new MailMessage(From, to);    //  

            message.Subject = "Сообщение Электронного Журанала. Успешная регистрация.";
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = false;


            message.Body = content;
            client.Send(message);
        }
    }
}